﻿using MySql.Data.MySqlClient;
using newBlog.CenterInc.model;
using newBlog.CenterInc.tool;
using newBlog.inc.SqlINC;
using System.Collections.Generic;

namespace newBlog.CenterInc.Ctrltor
{
    public class Pay_inc
    {
        sqlConnect Conn = new sqlConnect();
        isString isString = new isString();
        function function = new function();
        //定义所有支付函数

        public bool Deduction(string user_id, int price)
        {
            string sql = "update blog_user set Blog_user_Balance=Blog_user_Balance-{0} where Blog_user_id='{1}'";
            if (Conn.AddDelUp(string.Format(sql, price, user_id)) < 1)
            {
                return true;
            }
            return false;
        }//减少余额

        public bool increase(string user_id, int price) {
            string sql = "update blog_user set Blog_user_Balance=Blog_user_Balance+{0} where Blog_user_id='{1}'";
            if (Conn.AddDelUp(string.Format(sql, price, user_id)) < 1)
            {
                return true;
            }
            return false;
        }//增加余额
        public bool CreateOrder(string channel, string user_id, int price)
        {
            string sql = "insert into blog_order(Blog_order_channel,Blog_user_id,Blog_order_price) values('{0}','{1}','{2}')";
            if (Conn.AddDelUp(string.Format(sql, channel, user_id, price)) < 1)
            {
                return true;
            }
            return false;
        }//创建消费订单

        public bool CreateRecharge(string user_id,int price,string channel) {
            string sql = "insert into blog_recharge(Blog_user_id,Blog_Recharge_Price,Blog_Recharge_channel) values('{0}','{1}','{2}')";
            if (Conn.AddDelUp(string.Format(sql, user_id, price, channel)) < 1)
            {
                return true;
            }
            return false;
        
        }//创建充值订单
        public int getRecharge_Count(string user_id, string minPrice, string maxPrice, string minTime, string maxTime, string id)
        {
            string sql = "SELECT count(Blog_Recharge_id) FROM blog_recharge where Blog_Recharge_channel like '%%' ";

            if (user_id.Length != 0)
            {
                sql += $" and Blog_user_id='{user_id}' ";
            }
            if (minPrice.Length != 0)
            {
                sql += $" and Blog_Recharge_Price>='{minPrice}'";
            }
            if (maxPrice.Length != 0)
            {
                sql += $" and Blog_Recharge_Price<='{maxPrice}'";
            }
            if (minTime.Length != 0)
            {
                sql += $" and Blog_Recharge_time>='{minTime}'";
            }
            if (maxTime.Length != 0)
            {
                sql += $" and Blog_Recharge_time<='{maxTime}'";
            }
            if (id.Length != 0)
            {
                sql += $" and Blog_Recharge_id='%{id}%'";
            }
            return Conn.DataValue(sql);
        }//获取充值订单总数
        public List<Recharge_shopDao> getRecharge_items(string user_id, string minPrice, string maxPrice, string minTime, string maxTime, string id, int page)
        {
            List<Recharge_shopDao> list = new List<Recharge_shopDao>();
            string sql = "SELECT Blog_Recharge_id,Blog_user_id,Blog_Recharge_Price,Blog_Recharge_channel,Blog_Recharge_time FROM blog_recharge where Blog_Recharge_channel like '%%' ";

            if (user_id.Length != 0)
            {
                sql += $" and Blog_user_id='{user_id}' ";
            }
            if (minPrice.Length != 0)
            {
                sql += $" and Blog_Recharge_Price>='{minPrice}'";
            }
            if (maxPrice.Length != 0)
            {
                sql += $" and Blog_Recharge_Price<='{maxPrice}'";
            }
            if (minTime.Length != 0)
            {
                sql += $" and Blog_Recharge_time>='{minTime}'";
            }
            if (maxTime.Length != 0)
            {
                sql += $" and Blog_Recharge_time<='{maxTime}'";
            }
            if (id.Length != 0)
            {
                sql += $" and Blog_Recharge_id='{id}'";
            }
            sql += " order by Blog_Recharge_time desc ";
            sql += $"limit {page},20";
            MySqlDataReader dr = Conn.DataListAll(sql);
            while (dr.Read())
            {
                Recharge_shopDao rechargeDao = new Recharge_shopDao(dr.GetInt32(0), dr.GetInt32(1), dr.GetInt32(2), dr.GetString(3), dr.GetString(4));
                list.Add(rechargeDao);
            }
            return list;
        }//获取充值的订单

        public int getOrder_Count(string user_id, string minPrice, string maxPrice, string minTime, string maxTime, string id)
        {
            string sql = "SELECT count(Blog_order_id) FROM blog_order where Blog_order_channel like '%%' ";

            if (user_id.Length != 0)
            {
                sql += $" and Blog_user_id='{user_id}' ";
            }
            if (minPrice.Length != 0)
            {
                sql += $" and Blog_order_price>='{minPrice}'";
            }
            if (maxPrice.Length != 0)
            {
                sql += $" and Blog_order_price<='{maxPrice}'";
            }
            if (minTime.Length != 0)
            {
                sql += $" and Blog_order_time>='{minTime}'";
            }
            if (maxTime.Length != 0)
            {
                sql += $" and Blog_order_time<='{maxTime}'";
            }
            if (id.Length != 0)
            {
                sql += $" and Blog_order_id='%{id}%'";
            }
            return Conn.DataValue(sql);
        }//获取消费订单总数

        public List<Recharge_shopDao> getShop_items(string user_id, string minPrice, string maxPrice, string minTime, string maxTime, string id, int page)
        {
            List<Recharge_shopDao> list = new List<Recharge_shopDao>();
            string sql = "SELECT Blog_order_id,Blog_user_id,Blog_order_price,Blog_order_channel,Blog_order_time FROM blog_order where Blog_order_channel like '%%' ";

            if (user_id.Length != 0)
            {
                sql += $" and Blog_user_id='{user_id}' ";
            }
            if (minPrice.Length != 0)
            {
                sql += $" and Blog_order_price>='{minPrice}'";
            }
            if (maxPrice.Length != 0)
            {
                sql += $" and Blog_order_price<='{maxPrice}'";
            }
            if (minTime.Length != 0)
            {
                sql += $" and Blog_order_time>='{minTime}'";
            }
            if (maxTime.Length != 0)
            {
                sql += $" and Blog_order_time<='{maxTime}'";
            }
            if (id.Length != 0)
            {
                sql += $" and Blog_order_id='{id}'";
            }
            sql += " order by Blog_order_time desc ";
            sql += $"limit {page},20";
            MySqlDataReader dr = Conn.DataListAll(sql);
            while (dr.Read())
            {
                Recharge_shopDao rechargeDao = new Recharge_shopDao(dr.GetInt32(0), dr.GetInt32(1), dr.GetInt32(2), dr.GetString(3), dr.GetString(4));
                list.Add(rechargeDao);
            }
            return list;
        }//获取消费的订单

        public List<CarmichaelDao> carmichaels(int mode, int page)
        {
            string sql = "SELECT " +
                "Blog_Carmichael_id," +
                "ifnull(Blog_user_id,'未使用')," +
                "Blog_Carmichael_Value," +
                "Blog_Carmichael_Key," +
                "Blog_Carmichael_price," +
                "Blog_Carmichael_Createtime," +
                "ifnull(Blog_Carmichael_UseTime,'未使用') " +
                "FROM " +
                "`blog_carmichael` ";
            if (mode == 1)
            {
                sql += " where ISNULL(Blog_user_id)";
            }
            if (mode == 2)
            {
                sql += " where not ISNULL(Blog_user_id)";
            }
            sql += " limit {0},20";
            List<CarmichaelDao> list = new List<CarmichaelDao>();
            MySqlDataReader dr = Conn.DataListAll(string.Format(sql, page * 20));
            while (dr.Read())
            {
                CarmichaelDao carmichaelDao = new CarmichaelDao(dr.GetString(0), dr.GetString(1), dr.GetString(2), dr.GetString(3), dr.GetString(4), dr.GetString(5), dr.GetString(6));
                list.Add(carmichaelDao);
            }
            return list;
        }//卡密使用情况

        public int carmichaelsCount(int mode)
        {
            string sql = "SELECT count(Blog_Carmichael_id) FROM `blog_carmichael`";
            if (mode == 1)
            {
                sql += " where ISNULL(Blog_user_id)";
            }
            if (mode == 2)
            {
                sql += " where not ISNULL(Blog_user_id)";
            }
            return Conn.DataValue(sql);
        }

        public bool CreateKeyValue(int nums, int price)
        {
            int i = 0;
            string sql = "insert into blog_carmichael(Blog_Carmichael_Value,Blog_Carmichael_Key,Blog_Carmichael_price) values('{0}','{1}','{2}')";
            for (; i < nums; i++)
            {
                string value = function.CreateRanNum(10);
                string key = function.CreateRanNum(10);
                if (Conn.AddDelUp(string.Format(sql, value, key, price)) < 1)
                {
                    return true;
                }
            }
            return false;
        }
        public bool DelKeyValue(string id)
        {
            string sql = "delete from blog_carmichael where Blog_Carmichael_id={0}";
            if (Conn.AddDelUp(string.Format(sql, id)) < 1)
            {
                return true;
            }

            return false;
        }//删除卡密

        public bool isKeyValueUse(string key, string value) {
            string sql = "select count(Blog_carmichael_id) from blog_carmichael where Blog_Carmichael_Key='{0}' and Blog_Carmichael_Value='{1}' and not isnull(Blog_user_id)";
            if (Conn.DataValue(string.Format(sql,key,value))>0) {
                return true;
            }
            return false;
        }//卡密是否被使用
        public int getKeyValuePrice(string key, string value) {
            string sql = "select Blog_Carmichael_price from blog_carmichael where Blog_Carmichael_Key='{0}' and Blog_Carmichael_Value='{1}'";
            return Conn.DataValue(string.Format(sql,key,value));
        }

        public bool modifyKeyValueState(string user_id,string key, string value) {
            string sql = "update blog_carmichael set Blog_user_id='{0}',Blog_Carmichael_UseTime=now() where Blog_Carmichael_Key='{1}' and Blog_Carmichael_Value='{2}'";
            if (Conn.AddDelUp(string.Format(sql, user_id,key,value)) < 1)
            {
                return true;
            }

            return false;
        }
        
    }
}